package au.com.handsunited.web.controller.project;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.Project;
import au.com.handsunited.web.command.project.ProjectCommand;
import au.com.handsunited.web.session.SessionSupport;

public class ProjectRegistrationController extends AbstractProjectEditController {


	private static Logger logger = LoggerFactory.getLogger(ProjectRegistrationController.class);


	/**
	 *
	 */
	public ProjectRegistrationController() {
		setCommandClass(ProjectCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		ProjectCommand projectCommand = (ProjectCommand) command;
		Project project = projectCommand.getProject();

		BaseEntity owner = projectCommand.getOwnersMap().get(projectCommand.getOwnerId());

		if (owner == null) {
			owner = SessionSupport.getUser(request);
		}

		project = projectService.createProject(project, owner, null);

		Map<String, String> model = new HashMap<String, String>();
		model.put("projectName", project.getName());
		model.put("projectId", project.getId().toString());

		ModelAndView mav = new ModelAndView(getSuccessView(), model);
		return mav;
	}


}
